package com.example.test;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button myButton;
    private List<Fruit> fruitList = new ArrayList<Fruit>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button frames = (Button)findViewById(R.id.btn_frame);
        frames.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent().setClass(MainActivity.this,FrameTest.class);
                startActivity(intent);
            }
        });

        myButton=(Button)findViewById(R.id.main_button);
        //定义Toast显示时间属性
        final int S = Toast.LENGTH_SHORT;
        final int I = Toast.LENGTH_LONG;
        //显示Toast显示内容属性
        final String str = "Hello World!!!";
        //添加点击监听器
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建Toast
                Toast t = Toast.makeText(getApplicationContext(),str,S);
                //显示Toast
                t.show();
            }
        });

        initFruits();//初始化数据
        FruitAdapter adapter= new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        final String sta = "This is fruit!!!";
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog alertDialog1 = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("This is fruit!!!")
                        .setMessage("Hello World!")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"这是确定按钮",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,"这是取消按钮",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alertDialog1.show();
//                //创建Toast
//                Toast t = Toast.makeText(getApplicationContext(),sta,S);
//                //显示Toast
//                t.show();
            }
        });
    }

    private void initFruits(){
        Fruit apple = new Fruit("Apple",R.drawable.ic_launcher_background);
        fruitList.add(apple);
        Fruit banana = new Fruit("Banana",R.drawable.ic_launcher_foreground);
        fruitList.add(banana);
        Fruit orange = new Fruit("Orange",R.drawable.ic_launcher_foreground);
        fruitList.add(orange);
        Fruit pear = new Fruit("Pear",R.drawable.ic_launcher_foreground);
        fruitList.add(pear);
    }

    //跳转界面的逻辑代码
    public void Jump(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,MainActivity2.class);
        startActivity(intent);
    }

    public void Jumppicturepicker(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,PicturePicker.class);
        startActivity(intent);
    }

    public void Jumppicturepicker2(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,PicturePicker2.class);
        startActivity(intent);
    }

    public void JumptestSetting(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,testSetting.class);
        startActivity(intent);
    }
}